#include "poller.h"
#include "sleeperthread.h"

// timeout in seconds
#define POLL_SERVER_TIMEOUT 10
#define WAIT_FOR_CONNECTION 5

Poller::Poller(QTcpSocket *sc, int p, QString hn)
{
    serverConnection = sc;
    hostname = hn;
    port = p;
}

void Poller::pollServer(){

    while(true){

        // attempt to make a connection
        serverConnection->connectToHost(hostname, port);

        // wait for connection
        if(serverConnection->waitForConnected(WAIT_FOR_CONNECTION * 1000)){
            break;
        }else{
            qDebug() << "Trying to connect to" << hostname << "on port" << port << "-" << serverConnection->errorString();
        }

        // take some rest
        SleeperThread::sleep(POLL_SERVER_TIMEOUT);
    }

}
